package com.libinshen.wxpay.common.exception;

import com.libinshen.wxpay.common.enums.BaseResponseCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author wangzhibo
 */
@ControllerAdvice
@Slf4j
public class ExceptionAdvice {

    /**
     * 统一处理返回异常Exception
     *
     * @param e exception
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public BaseException handleException(Exception e) {
        e.printStackTrace();
        if (e instanceof BaseException) {
            BaseException baseException = (BaseException) e;
            log.error("异常类：" + this.getClass().getName());
            log.error(baseException.getMessage());
            return BaseException.fail(baseException.getCode(), baseException.getMsg());
        } else {
            log.error("异常类：" + this.getClass().getName());
            log.error(e.getMessage());
            return BaseException.fail(BaseResponseCodeEnum.ERROR_CODE.getCode(), BaseResponseCodeEnum.ERROR_CODE.getDesc());
        }
    }
}
